﻿using System;
using System.Text;
using StockLib.Properties;
using StockUtil;
using System.IO;
using System.Diagnostics;

namespace StockLib.Quote
{
    public class StockReceiverDriver
    {
        protected const int RCV_WORK_SENDMSG = 4;      // 工作方式类型定义-窗口消息方式

        protected const int RCV_DATA_MSG = 0x8001;     // 指定使用的消息

        protected static DllHelper _dllHelper = new DllHelper();

        protected static string GetDriverPath()
        {
            /*string path = Settings.Default.ReceiverDriverPath;
            if (!string.IsNullOrEmpty(path) && File.Exists(path))
                return path;*/

            return "Stock.dll";
        }

        public static bool LoadDriver()
        {
            return _dllHelper.LoadDll(GetDriverPath());
        }

        public static bool UnloadDriver()
        {
            return _dllHelper.UnloadDll();
        }

        public static bool Init(IntPtr hWnd)
        {
            DelegateStockInit func = (DelegateStockInit)_dllHelper.Invoke("Stock_Init", typeof(DelegateStockInit));

            return func(hWnd, RCV_DATA_MSG, RCV_WORK_SENDMSG) > 0;
        }

        public static bool Quit(IntPtr hWnd)
        {
            DelegateStockQuit func = (DelegateStockQuit)_dllHelper.Invoke("Stock_Quit", typeof(DelegateStockQuit));
            return func(hWnd) > 0;
        }

        public static bool Setup(bool bHide)
        {
            DelegateSetupReceiver func = (DelegateSetupReceiver)_dllHelper.Invoke("SetupReceiver", typeof(DelegateSetupReceiver));
            return func(Convert.ToInt32(bHide)) > 0;
        }

        public static string GetDriverInfo(int nType)
        {
            StringBuilder info = new StringBuilder();

            DelegateGetStockDriverInfo func = (DelegateGetStockDriverInfo)_dllHelper.Invoke("GetStockDriverInfo", typeof(DelegateGetStockDriverInfo));
            func(nType, info);

            return info.ToString();
        }

        private delegate int DelegateStockInit(IntPtr hWnd, UInt32 msg, int workMode);

        private delegate int DelegateStockQuit(IntPtr hWnd);

        private delegate int DelegateSetupReceiver(int nHide);

        private delegate int DelegateGetStockDriverInfo(int nType, StringBuilder strInfo);
    }
}
